package com.csii.modules.online.config.util;

import com.csii.core.util.SpringContextUtils;
import com.csii.core.util.oConvertUtils;
import com.csii.modules.online.config.db.DataBaseConfig;
import com.csii.modules.online.config.exception.DBException;
import com.csii.modules.online.config.service.DbTableHandler;
import com.csii.modules.online.config.service.impl.MysqlDbTableHandler;
import com.csii.modules.online.config.service.impl.OracleDbTableHandler;
import com.csii.modules.online.config.service.impl.PostgresqlDbTableHandler;
import com.csii.modules.online.config.service.impl.SqlServerDbTableHandler;
import org.hibernate.Session;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class DialectUtil {
	private static final Logger LOGGER = LoggerFactory.getLogger(DialectUtil.class);
	public static String databaseType = "";

	public static DbTableHandler getTableHandle() throws SQLException, DBException {
		DbTableHandler var0 = null;
		String var1 = getDatabaseType();
		switch(var1){
			case "MYSQL":
				var0 = new MysqlDbTableHandler();
				break;
			case "ORACLE":
				var0 = new OracleDbTableHandler();
				break;
			case "SQLSERVER":
				var0 = new SqlServerDbTableHandler();
				break;
			case "POSTGRESQL":
				var0 = new PostgresqlDbTableHandler();
		}

		return var0;
	}

	public static Connection getConnection() throws SQLException {
		DataSource dataSource = (DataSource) SpringContextUtils.getApplicationContext().getBean(DataSource.class);
		return dataSource.getConnection();
	}

	public static String getDatabaseType() throws SQLException, DBException {
		if (oConvertUtils.isNotEmpty(databaseType)) {
			return databaseType;
		} else {
			DataSource var0 = (DataSource) SpringContextUtils.getApplicationContext().getBean(DataSource.class);
			return getDataType(var0);
		}
	}

	public static boolean isOracle() {
		try {
			return "ORACLE".equals(getDatabaseType());
		} catch (SQLException var1) {
			var1.printStackTrace();
		} catch (DBException var2) {
			var2.printStackTrace();
		}

		return false;
	}

	public static String getDataType(DataSource dataSource) {
		if ("".equals(databaseType)) {
			try (Connection connection = dataSource.getConnection()){
				String database = connection.getMetaData().getDatabaseProductName().toLowerCase();
				if (database.indexOf("mysql") >= 0) {
					databaseType = "MYSQL";
				} else if (database.indexOf("oracle") >= 0) {
					databaseType = "ORACLE";
				} else if (database.indexOf("sqlserver") < 0 && database.indexOf("sql server") < 0) {
					if (database.indexOf("postgresql") < 0) {
						throw new DBException("数据库类型:[" + database + "]不识别!");
					}

					databaseType = "POSTGRESQL";
				} else {
					databaseType = "SQLSERVER";
				}
			} catch (Exception var7) {
				LOGGER.error(var7.getMessage(), var7);
			}
		}

		return databaseType;
	}

	public static String getDataType(Session var0) throws SQLException, DBException {
		return getDatabaseType();
	}

	public static String getTableName(String var0, String var1) {
		switch (var1) {
			case "ORACLE":
				return var0.toUpperCase();
			case "POSTGRESQL":
				return var0.toLowerCase();
			default:
				return var0;
		}
	}

	public static Boolean isTableExists(String var0) {
		Boolean flag;
		ResultSet resultSet = null;
		try(Connection connection =getConnection();) {
			String[] var3 = new String[]{"TABLE"};
			DatabaseMetaData var4 = connection.getMetaData();
			String var5 = var4.getDatabaseProductName().toUpperCase();
			String var6 = getTableName(var0, var5);
			DataBaseConfig var7 = (DataBaseConfig) SpringContextUtils.getBean(DataBaseConfig.class);
			String var8 = var7.getUsername();
			if ("ORACLE".equals(var5)) {
				var8 = var8.toUpperCase();
			}

			resultSet = var4.getTables(connection.getCatalog(), var8, var6, var3);
			if (resultSet.next()) {
				LOGGER.info("数据库表：【" + var0 + "】已存在");
				flag = true;
				return flag;
			}

			flag = false;
		} catch (SQLException var19) {
			throw new RuntimeException();
		}finally {
			if(resultSet!=null) {
				try {
					resultSet.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		}
		return flag;
	}

	public static Map<String, Object> a(List<Map<String, Object>> var0) {
		HashMap var1 = new HashMap();

		for (int var2 = 0; var2 < var0.size(); ++var2) {
			var1.put(((Map) var0.get(var2)).get("column_name").toString(), var0.get(var2));
		}

		return var1;
	}

	public static String getDialect() throws SQLException, DBException {
		return getDialect(getDatabaseType());
	}

	public static String getDialect(String databaseName) throws SQLException, DBException {
		String var1 = "org.hibernate.dialect.MySQL5InnoDBDialect";
		switch (databaseName) {
			case "SQLSERVER":
				var1 = "org.hibernate.dialect.SQLServerDialect";
				break;
			case "POSTGRESQL":
				var1 = "org.hibernate.dialect.PostgreSQLDialect";
				break;
			case "ORACLE":
				var1 = "org.hibernate.dialect.OracleDialect";
				break;
		}
		return var1;
	}

	public static String c(String var0) {
		return var0;
	}
}
